package com.moleq.mgnetwork;

public class PackageMessage {

	public String action;
	public String fromIp;
	public String toIp;
	public String station;
	public String fileName;
	public String dataContent;

	public String toCommand() {
		String result = "";
		result += "<Action>" + action + "</Action>";
		result += "<FromIP>" + fromIp + "</FromIP>";
		result += "<ToIP>" + toIp + "</ToIP>";
		result += "<Station>" + station+"</Station>";
		result += "<Content>" + dataContent+ "</Content>";

		return result;
	}

	public void parseCommand(String pkg) {

		action = this.getTagValue(pkg, "Action");
		fromIp = this.getTagValue(pkg, "FromIP");
		toIp = this.getTagValue(pkg, "ToIP");
		station = this.getTagValue(pkg, "Station");
		dataContent = this.getTagValue(pkg, "Content");

	}

	private String getTagValue(String str, String tag) {
		String result = "";
		String start, end;
		int i, j;

		if (str.equals("") || tag.equals(""))
			return result;

		start = "<" + tag + ">";
		end = "</" + tag + ">";

		i = str.indexOf(start);
		j = str.indexOf(end);
		if (i >= 0 && j >= 0) {
			result = str.substring(i + start.length(), j);
		}

		return result;

	}

}
